using System;
using Android.OS;
using Android.Views;
using Android.Widget;
using Android.Support.V4.App;
using Android.App;
using static Android.Widget.TabHost;
using Android.Runtime;

namespace LePcCtrlApp
{
    [Activity(Label = "CtrlActivity",Theme = "@android:style/Theme.Light.NoTitleBar")]
    public class CtrlActivity : FragmentActivity
    {
        private FragmentTabHost TabHost;
        private CtrlTabDb TabsDb;
        public void OnClick(View v)
        {
            throw new NotImplementedException();
        }

        protected override void OnCreate(Bundle bundle)
        {
            RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.CtrlLayout);
            TabHost = (FragmentTabHost)FindViewById(Android.Resource.Id.TabHost);
            TabHost.Setup(this,SupportFragmentManager, Resource.Id.contentLayout);
            TabHost.TabWidget.SetDividerDrawable(null);
            TabHost.TabChanged += Tabhost_TabChanged;
            TabsDb = new CtrlTabDb();
            initTab();
        }

        private void initTab()
        {
            string[] tabs = TabsDb.TabStrings;
            for (int i = 0; i < tabs.Length; i++)
            {
                TabSpec tabSpec = TabHost.NewTabSpec(tabs[i]).SetIndicator(GetTabView(i));
                TabHost.AddTab(tabSpec, TabsDb.Fragments[i], null);
                //TabHost.SetTag(i, Java.Lang.Integer.ValueOf(i));
                TabHost.Tag = i;
            }
        }

        private View GetTabView(int idx)
        {
            View view = LayoutInflater.Inflate(Resource.Layout.footer_tabs, null);
            //((TextView)view.FindViewById(Resource.Id.tvTab)).SetText(TabsDb.TabStrings[idx], BufferType.Normal);
            if (idx == 0)
            {
                //((TextView)view.FindViewById(Resource.Id.tvTab)).SetTextColor(Color.Red);
                ((ImageView)view.FindViewById(Resource.Id.ivImg)).SetImageResource(TabsDb.TabSelectImgs[idx]);
            }
            else
            {
                ((ImageView)view.FindViewById(Resource.Id.ivImg)).SetImageResource(TabsDb.TabImgs[idx]);
            }
            return view;
        }


        private void Tabhost_TabChanged(object sender, TabChangeEventArgs e)
        {
            TabWidget tabw = TabHost.TabWidget;
            for (int i = 0; i < tabw.ChildCount; i++)
            {
                View view = tabw.GetChildAt(i);
                ImageView iv = (ImageView)view.FindViewById(Resource.Id.ivImg);
                if (i == TabHost.CurrentTab)
                {
                    //((TextView)view.FindViewById(Resource.Id.tvTab)).SetTextColor(Color.Red);
                    iv.SetImageResource(TabsDb.TabSelectImgs[i]);
                }
                else
                {
                    //((TextView)view.FindViewById(Resource.Id.tvTab)).SetTextColor(ContextCompat.GetColorStateList(this, Resource.Color.foot_txt_gray));
                    iv.SetImageResource(TabsDb.TabImgs[i]);
                }
            }
        }

        public override void OnBackPressed()
        {
            StartActivity(typeof(MainActivity));
        }
    }
}